﻿using CommonEntity;
using CommonLibary;
using Enyim.Caching;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace NetCroe6WebAPITestOneBusiness
{
    public class MemcachedExpBusiness:BaseBusiness
    {
        private readonly IMemcachedClient _memcachedClient;
        public MemcachedExpBusiness(LoggerSingletonHelper loggerSingletonHelper, IMemcachedClient memcachedClient) : base(loggerSingletonHelper)
        {
            _memcachedClient = memcachedClient;
        }

        public ViewModelBase SetMemcachedValue(string key, string value) {
            return DoFunc<ViewModelBase>(() => {
                ViewModelBase vb = new ViewModelBase();
                bool b = _memcachedClient.Set(key, value, 10);

                vb.code = b ? HttpStatusCode.OK : HttpStatusCode.ExpectationFailed;
                vb.data = b;
                return vb;
            });
        }

        public ViewModelBase GetMemcachedValue(string key) {
            return DoFunc<ViewModelBase>(() => {
                ViewModelBase vb = new ViewModelBase();

                vb.data = _memcachedClient.Get(key);
                vb.code = HttpStatusCode.OK;

                return vb;
            });
        }
    }
}
